package com.softserveinc.moviedbapp.widget;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

import com.softserveinc.moviedbapp.application.MovieDBApplication;
import com.softserveinc.moviedbapp.manager.DataManager;
import com.softserveinc.moviedbapp.model.Movie;
import com.softserveinc.moviedbapp.util.MovieAppConstants;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UpdateService extends Service {
	private static final String TAG = UpdateService.class.getSimpleName();

	public static final String UPDATE = "widget_update";
	public static final String UPDATE_CONFIG_CHANGED = "widget_update_config_changed";
	public static final String CONFIGURE = "widget_configure";
	public static final String SHOW_MOVIE_DETAILS = "widget_movie_details";
	public static final String UPDATE_ITSELF = "widget_update_itself";

	private SharedPreferences mPrefs;
	private DataManager mDataProvider;
	private ExecutorService mExecutorService;
	private Map<Integer, Integer> mCurrentPosition;

	@Override
	public void onCreate() {
		super.onCreate();

		mPrefs = getSharedPreferences("widget_prefs", 0);
		mDataProvider = ((MovieDBApplication) getApplication()).getDataManager();
		mExecutorService = Executors.newSingleThreadExecutor();
		mCurrentPosition = new HashMap<Integer, Integer>();

	}

	@Override
	public int onStartCommand(final Intent intent, final int flags, final int startId) {
		mExecutorService.execute(new Runnable() {

			@Override
			public void run() {
				String action = intent.getAction();
				int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
				Log.d(TAG, "in onStartCommand() and action is " + action);
				Log.d(TAG, "in onStartCommand() and widget id is " + appWidgetId);

				/* if the user pressed configure button, launch config activity */
				if (action.equals(CONFIGURE)) {
					startActivity(intent);
					return;

				} else if (action.equals(UPDATE_CONFIG_CHANGED)) {
					Log.d(TAG, "in onStartCommand() updating configuration");
					int updateNextMovie = mPrefs.getInt("updateNextMovie" + appWidgetId, 5);
					int updateMovieList = mPrefs.getInt("updateMovieList" + appWidgetId, 30);
					String orderBy = mPrefs.getString("orderBy" + appWidgetId, "release");
					String results = mPrefs.getString("results" + appWidgetId, "30");

					Log.d(TAG, "in storeWidgetPreferences() prefs:\n");
					Log.d(TAG, "updateNextMovie " + updateNextMovie + "\n");
					Log.d(TAG, "updateMovieList " + updateMovieList + "\n");
					Log.d(TAG, "orderBy " + orderBy + "\n");
					Log.d(TAG, "results " + results + "\n");

					mDataProvider.provideMovieListForWidget(orderBy, results);
				}

				/* ordering preference and max results */
				String orderBy = mPrefs.getString("orderBy" + appWidgetId, "release");
				String results = mPrefs.getString("results" + appWidgetId, "30");

				Integer currentPosition = mCurrentPosition.get(appWidgetId);
				if (currentPosition == null || currentPosition == Integer.parseInt(results) - 1) {
					currentPosition = 0;
				} else {
					currentPosition++;
				}
				mCurrentPosition.put(appWidgetId, currentPosition);

				Log.d(TAG, "in onStartCommand() gonna get movie for position " + currentPosition + " and order "
						+ orderBy);
				Movie movie = mDataProvider.provideMovieForWidget(orderBy, currentPosition);

				Intent intentForceUpdate = new Intent("update.yourself.you.focker");
				intentForceUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
				intentForceUpdate.putExtra(MovieAppConstants.MOVIE_OBJECT, movie);
				Log.d(TAG, "in onStartCommand() got movie, sending broadcast");
				sendBroadcast(intentForceUpdate, "com.softserveinc.moviedbapp.permission.APP_WIDGET_RECEIVER");

			}

		});

		return Service.START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(final Intent intent) {
		return null;
	}

}